package com.zpz.framework.zpzoauth.common.utils;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * @author zhangpanzhi
 * @description
 * @create 2019-07-09 13:44
 */
public class ZpzOauthStringUtil {

    private static String firstNum = "1";
    private static String[] secondNumArray = {"3", "4", "5", "7", "8"};
    private static DecimalFormat amountFormat = new DecimalFormat("0.00");
    private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final Random RANDOM = new SecureRandom();

    public static String generatID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 获取随机字符串 Nonce Str
     *
     * @return String 随机字符串
     */
    public static String generateNonceStr(int len) {
        char[] nonceChars = new char[len];
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
        }
        return new String(nonceChars);
    }
    /**
     * 金额格式化
     * @param amount
     * @return
     */
    public static String amountFormat(BigDecimal amount) {
        if (amount != null && amount.compareTo(new BigDecimal("0")) != 0) {
            return amountFormat.format(amount);
        }
        return null;
    }

    public static String generatNum() {
        DateFormat formater = new SimpleDateFormat("yyMMddHHmmss");
        StringBuilder sb = new StringBuilder(formater.format(ZpzOauthDateUtils.convertGMT8(new Date())));
        return sb.toString();
    }

    /**
     * 生成最长18位的数字码
     * @return
     */
    public static String generatNum(String serial){
        StringBuilder orderId = new StringBuilder(serial);
        for(int i = 6 - serial.length(); i > 0; i--) {
            orderId.append("0");
        }
        orderId.append(generatNum());
        return orderId.toString();
    }

    /**
     * 调用一次生成一个手机号，手机号后9位数字通过循环生成
     */
    public static String getPhoneNo() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        String secondNum = secondNumArray[random.nextInt(secondNumArray.length)];
        sb.append(firstNum);
        sb.append(secondNum);
        for (int i = 0; i < 9; i++) {
            Integer thirdNum = random.nextInt(10);
            sb.append(thirdNum.toString());
        }
        return sb.toString();
    }

    /**
     * 当需要生成较大量的手机号时调用该方法
     * 后9位数字随机生成，长度不足时则补0
     */
    public static String getMultiPhoneNo() {
        StringBuffer sb = new StringBuffer();
        Random random = new Random();
        String secondNum = secondNumArray[random.nextInt(secondNumArray.length)];
        sb.append(firstNum);
        sb.append(secondNum);
        Integer thirdNum = 1 + random.nextInt(999999999);
        if (thirdNum.toString().length() <= 9) {
            sb.append(thirdNum);
            for (int i = 1; i <= 9 - thirdNum.toString().length(); i++) {
                sb.append(0);
            }
        } else {
            sb.append(thirdNum.toString());
        }
        return sb.toString();

    }

    /**
     * 手机号校验
     */
    public static boolean checkPhoneNo(String phoneNum) {
        // 定义手机号的规则
        String phoneNumPattern = "^1[34578][0-9]{9}";
        // 比对phoneNum是否符合定义的规则
        boolean result = Pattern.matches(phoneNumPattern, phoneNum);
        return result;

    }

    /**
     * 随机姓名生成
     * @author zhangpanzhi 2018年8月7日
     * @return
     */
    public static String getName() {
        Random random = new Random();
        String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
                "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
                "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷",
                "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和",
                "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒",
                "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"};
        String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
        String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
        int index = random.nextInt(Surname.length - 1);
        String name = Surname[index]; //获得一个随机的姓氏
        int i = random.nextInt(3);//可以根据这个数设置产生的男女比例
        if(i==2){
            int j = random.nextInt(girl.length()-2);
            if (j % 2 == 0) {
                name = name + girl.substring(j, j + 2);//"女-" + name + girl.substring(j, j + 2);
            } else {
                name = name + girl.substring(j, j + 1);//"女-" + name + girl.substring(j, j + 1);
            }

        }
        else{
            int j = random.nextInt(girl.length()-2);
            if (j % 2 == 0) {
                name = name + boy.substring(j, j + 2);//"男-" + name + boy.substring(j, j + 2);
            } else {
                name = name + boy.substring(j, j + 1);//"男-" + name + boy.substring(j, j + 1);
            }

        }

        return name;
    }

    /**
     * 字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isNullOrEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }

}
